﻿using JuCheap.Core.Infrastructure;
using JuCheap.Core.Infrastructure.Extentions;
using JuCheap.Core.IRepository;
using JuCheap.Core.IService.AppService;
using JuCheap.Core.Model;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace JuCheap.Core.Service.AppService
{
    /// <summary>
    /// 产品查询服务
    /// </summary>
    public class ProductQueryService : IProductQueryService
    {
        private readonly IProductRepository _productRepository;
        private readonly IProductCategoryRepository _productCategoryRepository;

        /// <summary>
        /// ctor
        /// </summary>
        public ProductQueryService(IProductRepository productRepository, IProductCategoryRepository productCategoryRepository)
        {
            _productRepository = productRepository;
            _productCategoryRepository = productCategoryRepository;
        }

        /// <summary>
        /// 产品分页扩展方法
        /// </summary>
        /// <param name="filter">基础分页参数</param>
        /// <returns></returns>
        public async Task<PagedResult<ProductDto>> QueryPagesAsync(ProductFilter filter)
        {
            var query = from product in _productRepository.Queryable()
                        join cate in _productCategoryRepository.Queryable() on product.CategoryId equals cate.Id into cates
                        from cate in cates.DefaultIfEmpty()
                        select new ProductDto
                        {
                            Id = product.Id,
                            ProductName = product.ProductName,
                            ProductCode = product.ProductCode,
                            CategoryId = product.CategoryId,
                            RetailPrice = product.RetailPrice,
                            SalePrice = product.SalePrice,
                            UnitName = product.UnitName,
                            Quantity = product.Quantity,
                            IsShowInHome = product.IsShowInHome,
                            IsPublish = product.IsPublish,
                            Sort = product.Sort,
                            CreateTime = product.CreateTime,
                            CategoryName = cate != null ? cate.CategoryName : "",
                            IsCategoryPublish = cate != null ? cate.IsPublish : false,
                        };
            var categoryIds = new List<string>();
            if (filter.CategoryId.IsNotBlank())
            {
                categoryIds = await _productCategoryRepository.Queryable().Where(x => x.ParentId == filter.CategoryId)
                    .Select(x => x.Id).ToListAsync();
                categoryIds.Add(filter.CategoryId);
            }
            query = query.WhereIf(filter.Keywords.IsNotBlank(), (x => x.ProductName.Contains(filter.Keywords) || x.ProductCode == filter.Keywords))
                .WhereIf(categoryIds.AnyOne(), x => categoryIds.Contains(x.CategoryId));

            return await query.QueryPagesAsync(filter);
        }
    }
}
